package com.svs.web.controller.api;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.svs.common.base.AjaxResult;
import com.svs.common.utils.DateUtils;
import com.svs.framework.web.BaseController;
import com.svs.project.domain.ProLine;
import com.svs.project.domain.ProLineLog;
import com.svs.project.service.ProLineLogService;
import com.svs.project.service.ProLineService;
import com.svs.system.domain.SysDictData;
import com.svs.system.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/api/screen")
public class ApiScreenController extends BaseController {

    private String prefix = "project/screen";

    @Autowired
    private ProLineLogService proLineLogService;

    @Autowired
    private ProLineService proLineService;

    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping("/index")
    public String screen(String monitorCode, ModelMap mmap) {
        List<ProLineLog> list = proLineLogService.screenList(monitorCode);
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("run_state");
        List<SysDictData> dictList = sysDictDataService.selectAllList(sysDictData);
        HashMap<String, String> map = new HashMap<>();
        for (SysDictData dict:dictList) {
            map.put(dict.getDictValue(),dict.getDictLabel());
        }
        for (ProLineLog log:list) {
            if(log.getRealNum() == null || log.getRealNum() == 0){
                ProLineLog proLineLog = new ProLineLog();
                proLineLog.setNowTime(DateUtil.yesterday());
                proLineLog.setLineCode(log.getLineCode());
                List<ProLineLog> l = proLineLogService.selectAllList(proLineLog);
                if(!l.isEmpty()){
                    log.setRealNum(l.get(0).getRealNum());
                    log.setReachRate(l.get(0).getReachRate());
                }
            }
            log.setRunState(map.get(log.getRunState()));
        }
        mmap.put("monitorList", list);
        return prefix+"/index";
    }

    @GetMapping("/enter")
    public String enter(ModelMap mmap) {
        //查询需要手工录入的生产线
        mmap.put("counterList", proLineService.counterList());
        return prefix+"/enter";
    }

    @PostMapping("/enter/select")
    @ResponseBody
    public AjaxResult enterSelect(ProLineLog proLineLog) {
        //判断当前时间是否在9点前
        if(DateUtil.between(DateUtil.date(),DateUtil.parseTimeToday("09:00:00"),DateUnit.MS,false) > 0){
            proLineLog.setNowTime(DateUtil.yesterday());
        }else{
            proLineLog.setNowTime(DateUtil.date());
        }
        List<ProLineLog> list = proLineLogService.selectAllList(proLineLog);
        if(list.isEmpty()){
            return AjaxResult.success("",proLineLog);
        }
        return AjaxResult.success("",list.get(0));
    }

    public static void main(String[] args) {
//        System.out.println(DateUtil.date());
//        System.out.println(DateUtil.yesterday());
//        System.out.println();
    }

    @PostMapping("/enter/update")
    @ResponseBody
    public AjaxResult enterUpdate(ProLineLog proLineLog) {
        //判断当前时间是否在9点前
        if(DateUtil.between(DateUtil.date(),DateUtil.parseTimeToday("09:00:00"),DateUnit.MS,false) > 0){
            proLineLog.setNowTime(DateUtil.yesterday());
        }else{
            proLineLog.setNowTime(DateUtil.date());
        }
        ProLine proLine = proLineService.selectByCode(proLineLog.getLineCode());
        proLineLog.setLineId(proLine.getId());
        proLineLog.setLineName(proLine.getLineName());
        proLineLog.setPlanNum(proLine.getPlanNum());
        String reachRate = new BigDecimal((double)proLineLog.getRealNum()*100/proLineLog.getPlanNum()).setScale(2,BigDecimal.ROUND_HALF_UP)+"%";
        proLineLog.setReachRate(reachRate);
        proLineLog.setRunState(proLine.getRunState());

        ProLineLog log = new ProLineLog();
        log.setLineCode(proLineLog.getLineCode());
        log.setNowTime(proLineLog.getNowTime());
        List<ProLineLog> list = proLineLogService.selectAllList(log);
        if(!list.isEmpty()){
            return toAjax(proLineLogService.update(proLineLog));
        }else{
            proLineLog.setId(null);
            return toAjax(proLineLogService.save(proLineLog));
        }
    }
}
